package yunquan.wang.base.process;

import java.util.List;
import java.util.Map;

import org.jbpm.task.service.UserGroupCallback;

import yunquan.wang.base.spring.SpringContextHolder;
import yunquan.wang.base.spring.security.SecurityService;

public class JpaUserGroupCallback implements UserGroupCallback {

	private Map<String, List<String>> userGroups;
	private List<String> groupNames;

	@Override
	public boolean existsUser(String userId) {
		checkInit();
		return userGroups.containsKey(userId);
	}

	@Override
	public boolean existsGroup(String groupId) {
		checkInit();
		return groupNames.contains(groupId);
	}

	@Override
	public List<String> getGroupsForUser(String userId, List<String> groupIds,
			List<String> allExistingGroupIds) {
		checkInit();
		return userGroups.get(userId);
	}

	/**
	 * 初始化数据
	 */
	private void checkInit() {
		if (userGroups == null || groupNames == null) {
			SecurityService securityService = SpringContextHolder
					.getBean(SecurityService.class);
			userGroups = securityService.getAllUserGroups();
			groupNames = securityService.getAllGroupNames();
		}
	}
}
